package com.travel.lib.base.retrofit;


import com.travel.lib.base.utils.TextCheckUtils;
import com.travel.lib.base.utils.ToastUtils;

import retrofit2.adapter.rxjava.HttpException;

/**
 * Created by spc on 2017/4/26.
 */

public class ResponseStatusErrorManager {
    //  服务器 返回status 判断
    public static void excute(String status, String msg) {
        switch (status) {
            default:
                if (TextCheckUtils.isUseable(msg))
                    ToastUtils.show(msg);
                break;
        }

    }


    //网络连接失败的处理
    public static void exceptionHandler(Throwable e) {
        String msg = "网络不太顺畅~";
        if (e instanceof ApiException) {
            //ApiException 是自己定义的 服务器返回 status 不为 0的异常。
            msg = ((ApiException) e).msg;
        } else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            int code = httpException.code();
            if (code == 504) {
                msg = "网络不给力";
            }
            if (code == 401) {
                msg = "没有凭证或凭证不正确";
            }

            if (code == 502 || code == 404) {
                msg = "服务器异常，请稍后再试";
            }
        }
        e.printStackTrace();
        ToastUtils.show(msg);
    }
}
